package com.lambda.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityOptions;
import android.os.Build;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Utils {
    public interface TranslucentConversionListener {
        void onTranslucentConversionComplete(boolean complete);
    }

    static class TranslucentConversionListenerDelegate implements InvocationHandler {
        WeakReference<TranslucentConversionListener> reference;

        @Override
        public final Object invoke(Object obj, Method method, Object[] params) {
            if (reference != null) {
                TranslucentConversionListener listener = reference.get();
                if (listener != null) {
                    if (params != null && params.length > 0 && (params[0] instanceof Boolean)) {
                        listener.onTranslucentConversionComplete((Boolean) params[0]);
                    }
                } else {
                    LogUtils.log("MicroMsg.ActivityUtil", "swipe invoke fail, callback NULL!");
                }
            } else {
                LogUtils.log("MicroMsg.ActivityUtil", "swipe invoke fail, callbackRef NULL!");
            }
            return null;
        }
    }

    public static void convertFromTranslucent(Activity activity) {
        try {
            Method declaredMethod = Activity.class.getDeclaredMethod("convertFromTranslucent");
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(activity);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @TargetApi(16)
    public static void convertToTranslucent(Activity activity, TranslucentConversionListener listener) {
        try {
            Class[] declaredClasses = Activity.class.getDeclaredClasses();
            Class cls = null;
            for (Class clss : declaredClasses) {
                if (clss.getSimpleName().contains("TranslucentConversionListener")) {
                    cls = clss;
                    break;
                }
            }

            Object newProxyInstance = null;
            if (listener != null) {
                TranslucentConversionListenerDelegate delegate = new TranslucentConversionListenerDelegate();
                delegate.reference = new WeakReference<>(listener);
                newProxyInstance = Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, delegate);
            }
            Method declaredMethod;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                declaredMethod = Activity.class.getDeclaredMethod("convertToTranslucent", cls, ActivityOptions.class);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(activity, newProxyInstance, null);
            } else {
                declaredMethod = Activity.class.getDeclaredMethod("convertToTranslucent", cls);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(activity, newProxyInstance);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
